import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/wxlogin'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  }]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/workorder',
    component: Layout,
    name: 'Workorder',
    meta: { title: '工单', icon: 'workorder', roles: ['admin', 'user'] },
    children: [
      {
        path: 'add',
        name: 'WorkorderAdd',
        component: () => import('@/views/flow/add'),
        roles: ['admin', 'user'],
        meta: { title: '新建工单' }
      },
      {
        path: 'list',
        name: 'WorkorderList',
        component: () => import('@/views/flow/index'),
        roles: ['admin', 'user'],
        meta: { title: '工单记录' }
      },
      {
        path: 'detail',
        name: 'WorkorderDetail',
        component: () => import('@/views/flow/detail'),
        roles: ['admin', 'user'],
        meta: { title: '工单详情' },
        hidden: true
      }
    ]
  },

  // {
  //   path: '/restart',
  //   component: Layout,
  //   name: 'Restart',
  //   meta: { title: '重启工单', icon: 'restart', roles: ['admin', 'user'] },
  //   children: [
  //     {
  //       path: 'add',
  //       name: 'RestartAdd',
  //       component: () => import('@/views/flow/restart/add'),
  //       roles: ['admin', 'user'],
  //       meta: { title: '新建工单' }
  //     },
  //     {
  //       path: 'list',
  //       name: 'RestartList',
  //       component: () => import('@/views/flow/restart/index'),
  //       roles: ['admin', 'user'],
  //       meta: { title: '重启记录' }
  //     },
  //     {
  //       path: 'detail',
  //       name: 'RestartDetail',
  //       component: () => import('@/views/flow/restart/detail'),
  //       roles: ['admin', 'user'],
  //       meta: { title: '重启详情' },
  //       hidden: true
  //     }
  //   ]
  // },
  // {
  //   path: '/scale',
  //   component: Layout,
  //   name: 'Scale',
  //   meta: { title: '伸缩工单', icon: 'scale', roles: ['admin', 'user'] },
  //   children: [
  //     {
  //       path: 'add',
  //       name: 'ScaleAdd',
  //       component: () => import('@/views/flow/scale/add'),
  //       meta: { title: '新建工单' }
  //     },
  //     {
  //       path: 'list',
  //       name: 'ScaleList',
  //       component: () => import('@/views/flow/scale/index'),
  //       meta: { title: '伸缩记录' }
  //     },
  //     {
  //       path: 'detail',
  //       name: 'ScaleDetail',
  //       component: () => import('@/views/flow/scale/detail'),
  //       meta: { title: '伸缩详情' },
  //       hidden: true
  //     }
  //   ]
  // },
  // {
  //   path: '/regrade',
  //   component: Layout,
  //   name: 'Regrade',
  //   meta: { title: '变配工单', icon: 'scale', roles: ['admin', 'user'] },
  //   children: [
  //     {
  //       path: 'add',
  //       name: 'RegradeAdd',
  //       component: () => import('@/views/flow/regrade/add'),
  //       meta: { title: '新建工单' }
  //     },
  //     {
  //       path: 'list',
  //       name: 'RegradeList',
  //       component: () => import('@/views/flow/regrade/index'),
  //       meta: { title: '变配记录' }
  //     },
  //     {
  //       path: 'detail',
  //       name: 'RegradeDetail',
  //       component: () => import('@/views/flow/regrade/detail'),
  //       meta: { title: '变配详情' },
  //       hidden: true
  //     }
  //   ]
  // },
  // {
  //   path: '/deploy',
  //   component: Layout,
  //   name: 'Deploy',
  //   meta: { title: '发布工单', icon: 'el-icon-s-promotion' },
  //   children: [
  //     {
  //       path: 'add',
  //       name: 'DeployAdd',
  //       component: () => import('@/views/flow/deploy/add'),
  //       meta: { title: '新建工单' }
  //     },
  //     {
  //       path: 'list',
  //       name: 'DeployList',
  //       component: () => import('@/views/flow/deploy/index'),
  //       meta: { title: '发布管理' }
  //     },
  //     {
  //       path: 'detail',
  //       name: 'DeployDetail',
  //       component: () => import('@/views/flow/deploy/detail'),
  //       meta: { title: '发布详情' },
  //       hidden: true
  //     }
  //   ]
  // },
  {
    path: '/multideploy',
    component: Layout,
    name: 'MultiDeploy',
    alwaysShow: true,
    meta: { title: '批量发布', icon: 'multideploy' },
    children: [
      {
        path: 'list',
        name: 'MultiDeployList',
        component: () => import('@/views/flow/multideploy/index'),
        meta: { title: '批量发布' }
      },
      {
        path: 'detail',
        name: 'MultiDeployDetail',
        component: () => import('@/views/flow/multideploy/detail'),
        meta: { title: '发布详情' },
        hidden: true
      }
    ]
  },
  {
    path: '/flow',
    component: Layout,
    name: 'Flow',
    meta: { title: 'CD配置', icon: 'el-icon-s-promotion', roles: ['admin'] },
    children: [

      //   ]
      // },
      // {
      //   path: '/setting',
      //   component: Layout,
      //   name: 'Setting',
      //   meta: { title: '配置管理', icon: 'el-icon-s-tools' },
      //   children: [
      {
        path: 'business',
        name: 'BusinessSetting',
        component: () => import('@/views/setting/business/index'),
        meta: { title: '业务线配置' },
        children: [

        ]
      },
      {
        path: 'business/setting',
        name: 'BusinessSettingDetail',
        component: () => import('@/views/setting/business/detail'),
        meta: { title: '业务线配置编辑', layout: Layout },
        hidden: true
      },
      {
        path: 'project',
        name: 'ProjectSettingList',
        component: () => import('@/views/setting/project/index'),
        meta: { title: '项目配置' },
        children: [

        ]
      },
      {
        path: 'project/setting',
        name: 'ProjectSettingDetail',
        component: () => import('@/views/setting/project/detail'),
        meta: { title: '项目配置编辑' },
        hidden: true
      },
      {
        path: 'service',
        name: 'ServiceSetting',
        component: () => import('@/views/setting/service/index'),
        meta: { title: '服务配置' },
        children: [

        ]
      },
      {
        path: 'service/setting',
        name: 'ServiceSettingDetail',
        component: () => import('@/views/setting/service/detail'),
        meta: { title: '服务配置编辑' },
        hidden: true
      },
      {
        path: 'capacity',
        name: 'capacitySetting',
        component: () => import('@/views/setting/capacity/index'),
        meta: { title: '计算方案' }
      }, {
        path: 'yamltemplate',
        name: 'YamlTemplate',
        component: () => import('@/views/setting/yamltemplate/index'),
        meta: { title: '容器部署模板' }
      },
      {
        path: 'yamltemplate/setting',
        name: 'YamlTemplateEdit',
        component: () => import('@/views/setting/yamltemplate/edit'),
        meta: { title: '容器部署模板详情' },
        hidden: true
      }
    ]
  },
  {
    path: '/eff',
    component: Layout,
    name: 'Eff',
    meta: { title: '能效管理', icon: 'xiaoshuai', roes: ['admin', 'user'] },
    alwaysShow: true,
    children: [
      {
        path: 'gitlab',
        name: 'Gitlab',
        component: () => import('@/views/eff/gitlab/index'),
        meta: { title: 'Git统计' }
      },
      {
        path: 'tapd/services',
        name: 'TapdServices',
        component: () => import('@/views/eff/tapd/index'),
        hidden: true,
        meta: { title: 'Tapd关联应用' }
      },
      {
        path: 'tapd/tplans',
        name: 'TapdPlans',
        component: () => import('@/views/eff/tapd/tplan'),
        meta: { title: 'Tapd测试计划统计' }
      },
      {
        path: 'imagediff',
        name: 'ImageDiff',
        component: () => import('@/views/eff/imagediff/index'),
        meta: { title: '镜像比对' }
      },
      {
        path: 'deployment',
        name: 'Deployment',
        component: () => import('@/views/eff/deployment/index'),
        hidden: true,
        meta: { title: 'Deployment' }
      }
    ]
  },
  {
    path: '/resource',
    component: Layout,
    name: 'Resource',
    // redirect: '/resource/service',
    meta: { title: '资源管理', icon: 'el-icon-s-grid', roles: ['admin'] },
    children: [
      {
        path: 'lob',
        name: 'LineOfBusiness',
        component: () => import('@/views/resource/index'),
        meta: { title: '业务资源' },
        children: [
          // {
          //   path: 'cate',
          //   component: () => import('@/views/resource/cate/index'),
          //   name: 'Cate',
          //   meta: { title: '服务类别' }
          // },
          {
            path: 'business',
            name: 'Business',
            component: () => import('@/views/resource/business/index'),
            meta: { title: '业务线' }
          },
          {
            path: 'business/perm',
            name: 'BusinessPerm',
            component: () => import('@/views/resource/business/perm'),
            meta: { title: '业务线权限' },
            hidden: true
          },
          {
            path: 'project',
            name: 'Project',
            component: () => import('@/views/resource/project/index'),
            meta: { title: '项目' }
          },
          {
            path: 'service',
            name: 'Service',
            component: () => import('@/views/resource/service/index'),
            meta: { title: '服务' }
          }
        ]
      },
      {
        path: 'container',
        name: 'Container',
        component: () => import('@/views/resource/index'),
        meta: { title: '容器资源' },
        children: [
          {
            path: 'kube',
            name: 'Kube',
            component: () => import('@/views/resource/kube/index'),
            meta: { title: '集群管理' }
          },
          {
            path: 'repo',
            name: 'Repo',
            component: () => import('@/views/resource/repo/index'),
            meta: { title: '仓库管理' }
          }
        ]
      },
      {
        path: 'env',
        name: 'Env',
        component: () => import('@/views/resource/env/index'),
        meta: { title: '环境资源' }
      }
    ]
  },
  {
    path: '/system',
    component: Layout,
    name: 'System',
    redirect: '/system/menu',

    meta: { title: '系统管理', icon: 'el-icon-setting', roles: ['admin'] },
    children: [
      {
        path: 'menu',
        name: 'Menu',
        component: () => import('@/views/system/menu/index'),
        meta: { title: '菜单管理', icon: 'el-icon-menu' },
        hidden: true
      },
      {
        path: 'role',
        name: 'Role',
        component: () => import('@/views/system/role/index'),
        meta: { title: '角色管理', icon: 'el-icon-s-custom' }
      },
      {
        path: 'group',
        name: 'Group',
        hidden: true,
        component: () => import('@/views/system/menu/index'),
        meta: { title: '用户组管理' }
      },
      {
        path: 'user',
        name: 'User',
        component: () => import('@/views/system/user/index'),
        meta: { title: '用户管理', icon: 'el-icon-user' }
      },
      {
        path: 'config',
        name: 'Config',
        component: () => import('@/views/system/config/index'),
        meta: { title: '系统配置', icon: 'el-icon-setting' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
